package com.example.testcabbageapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.ListActivity;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;

public class CheckInView extends ListActivity {

	static ArrayList<String> checkinList = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// has something to do with allowing internet connections/threading/something
		StrictMode.ThreadPolicy policy = new StrictMode.
				ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);


		///
		this.setTitle("All checkin beans...");

		String readFeed = readCheckinFeed();
		try {
			JSONArray jsonArray = new JSONArray(readFeed);
			Log.i(CheckInView.class.getName(),
					"Number of entries " + jsonArray.length());
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String json = jsonObject.toString();
				Gson gson = new Gson();
				CheckinBeanReal bean = gson.fromJson(json,CheckinBeanReal.class);
				checkinList.add(bean.getName() + " " + bean.getUrlstring() + " " + bean.getCheckinDate() + " " + bean.getCheckinTime());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		// ListActivity uses an adapter to populate it's list
		ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.activity_list_item, android.R.id.text1,checkinList);
		this.setListAdapter(aa);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuMaker mm = new MenuMaker();
    	menu = mm.makeMenuItems(getApplicationContext(),menu);		
    	getMenuInflater().inflate(R.menu.activity_check_in_view, menu);
		return true;
	}

	public String readCheckinFeed() {
		StringBuilder builder = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet("http://playcabbage.net/rest/checkins/getjson");
		try {
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) {
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
				}
			} else {
				Log.e(CheckInView.class.toString(), "Failed to download file");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return builder.toString();
	}

}
